package eoeProject.edenOfEast.page.analyses.analysesRes.component;

import java.util.Date;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import eoeProject.edenOfEast.bean.EOEAnalysesResBean;
import eoeProject.edenOfEast.core.DBConnector;
import eoeProject.edenOfEast.core.DBUtils;
import eoeProject.edenOfEast.core.EOEAuthenticatedWebSession;
import eoeProject.edenOfEast.core.SQLMaker;
import eoeProject.edenOfEast.core.ThumbMaker;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES_RES;
import eoeProject.edenOfEast.utils.AnalysesUtils;

//@AuthorizeInstantiation("ADMIN")
public class AnalysesResFormPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private Form<Void> form = new Form<Void>("form");

	private Model<String> newRes = new Model<String>("");

	private FeedbackPanel feedback = new FeedbackPanel("feedback");

	private Label message = new Label("message", "");

	private Integer analysesId;

	public AnalysesResFormPanel(String id, Integer analysesId) {
		super(id);
		this.analysesId = analysesId;
		feedback.setOutputMarkupId(true);
		message.setOutputMarkupId(true);
		message.setVisible(false);

		TextArea<String> resFormArea = new TextArea<String>("resFormArea",
				newRes);
		resFormArea.setRequired(true);
		form.add(resFormArea);

		form.add(makeResButton);
		form.add(feedback);
		form.add(message);
		add(form);
	}

	private AjaxButton makeResButton = new AjaxButton("makeResButton") {
		/**
		 *
		 */
		private static final long serialVersionUID = -2942520388218435030L;

		@SuppressWarnings("rawtypes")
		@Override
		protected void onSubmit(AjaxRequestTarget target, Form form) {
			setResponsePage(form.getPage());
			ThumbMaker.graphics2d();

			EOEAnalysesResBean bean = new EOEAnalysesResBean();
			bean.setResId(DBUtils.getNextId(DB_EOE_ANALYSES_RES.TABLE_NAME,
					DB_EOE_ANALYSES_RES.PRIMARY_KEY));
			bean.setCreateUserId(EOEAuthenticatedWebSession.getUserId());
			bean.setRes(newRes.getObject());
			bean.setAnalysesId(analysesId);
			bean.setResNum(AnalysesUtils.getNextResNum(analysesId));
			bean.setCreateDate(new Date());
			bean.setUpdateDate(new Date());
			bean.setGJ(1);
			SQLMaker sql = new SQLMaker(DB_EOE_ANALYSES_RES.TABLE_NAME,
					SQLMaker.SQL_TYPE_INSERT);
			sql.setInsert(bean);

			SQLMaker sql2 = new SQLMaker(DB_EOE_ANALYSES.TABLE_NAME,
					SQLMaker.SQL_TYPE_UPDATE);
			sql2.addSetValue(DB_EOE_ANALYSES.TOTAL_RES, AnalysesUtils
					.getAnalysesBean(analysesId).getTotalRes() + 1);
			sql2.addExpEquals(DB_EOE_ANALYSES.ANALYSES_ID, analysesId);
			if (DBConnector.getInstance().set(sql.getSQL())
					&& DBConnector.getInstance().set(sql2.getSQL())) {
				PageParameters parameters = new PageParameters();
				parameters.add("analysesId", analysesId);
				setResponsePage(new eoeProject.edenOfEast.page.analyses.analysesRes.Index(
						parameters));
			} else {
				message = new Label("message", "*登録に失敗しました。");
				message.setOutputMarkupId(true);
				message.setVisible(true);
				form.addOrReplace(message);
			}

		}

		@SuppressWarnings({ "rawtypes" })
		@Override
		protected void onError(final AjaxRequestTarget target, final Form form) {
			target.add(feedback);
		}
	};

}
